package com.tanguy.user.ui.trip.choice.activity

import android.app.Activity
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.text.TextUtils
import android.widget.EditText
import com.jakewharton.rxbinding2.widget.RxTextView
import com.tanguy.user.R
import com.tanguy.user.base.ImmersionTitleActivity
import com.tanguy.user.ui.trip.choice.adapter.FlightAdapter
import com.tanguy.user.ui.window.OnePickerViewPopupWindow
import com.tanguy.user.utlis.TimeUtils
import com.tanguy.user.utlis.extend.hideOrShowKeyboard
import com.tanguy.user.utlis.extend.showPopupWindow
import org.jetbrains.anko.find

/**
 * Created by dab on 2017/10/30 0030 09:55
 * 选择航班的
 */
class ChoiceFlightActivity : ImmersionTitleActivity() {
    override fun setContentViewRes(): Int = R.layout.activity_choice_flight
    private val mRvContent by lazy { find<RecyclerView>(R.id.rv_content) }
    private val mETInput by lazy { find<EditText>(R.id.et_input) }
    private val mFlightList = ArrayList<String>()
    private val mFlightAdapter = FlightAdapter(mFlightList)
    override fun initView() {
        super.initView()
        title = "选择航班"
        mRvContent.apply {
            layoutManager = LinearLayoutManager(this@ChoiceFlightActivity)
            adapter = mFlightAdapter
        }
    }

    override fun initEvent() {
        super.initEvent()
        RxTextView.textChanges(mETInput)
                .subscribe({
                    mFlightList.clear()
                    val inputMessage = it.toString().replace(" ", "")
                    if (!TextUtils.isEmpty(inputMessage) && inputMessage.length > 3) {
                        mFlightList.add(it.toString())
                    }
                    mFlightAdapter.notifyDataSetChanged()
                }) {
                    it.printStackTrace()
                }

        mFlightAdapter.itemClick = { _, _,_ ->
            hideOrShowKeyboard(true)
            showPopupWindow(OnePickerViewPopupWindow(this,
                    arrayListOf("昨天" + TimeUtils.getDays(-1, "MM月dd日")
                            , "今天" + TimeUtils.getDays(0, "MM月dd日")
                            , "明天" + TimeUtils.getDays(1, "MM月dd日")
                            , "后天" + TimeUtils.getDays(2, "MM月dd日")
                    )
            ) { _, msg ->
                queryFlight(msg)
            }
            )
        }
    }

    /**
     * 查询航班
     */
    private fun queryFlight(msg: String?) {
        val intent = Intent().apply {
            putExtra("name", "测试机场")
            putExtra("time", msg)
            putExtra("flight_number", com.tanguy.user.utlis.extend.getEditTextString(mETInput))

        }
        setResult(Activity.RESULT_OK, intent)
        finish()
    }
}
